package com.example.temp.controller;

import com.example.temp.common.Result;
import com.example.temp.entity.QuestionCategory;
import com.example.temp.mapper.QuestionCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 问题分类管理控制器
 */
@RestController
@RequestMapping("/question-category")
@CrossOrigin(origins = "http://localhost:8081", allowedHeaders = "*")
public class QuestionCategoryController {

    @Autowired
    private QuestionCategoryMapper questionCategoryMapper;

    /**
     * 获取所有问题分类
     */
    @GetMapping("/list")
    public Result getAllCategories() {
        try {
            System.out.println("开始获取问题分类列表...");
            List<QuestionCategory> categories = questionCategoryMapper.selectList();
            System.out.println("查询到的分类数量: " + (categories != null ? categories.size() : 0));
            if (categories == null) {
                System.out.println("分类列表为null");
                return Result.success(new ArrayList<>());
            }
            return Result.success(categories);
        } catch (Exception e) {
            System.err.println("获取问题分类列表异常: " + e.getMessage());
            e.printStackTrace();
            return Result.fail("获取问题分类列表失败: " + e.getMessage());
        }
    }

    /**
     * 添加新的问题分类
     */
    @PostMapping("/add")
    public Result addCategory(@RequestBody QuestionCategory category) {
        try {
            if (category.getQuestionCategory() == null || category.getQuestionCategory().trim().isEmpty()) {
                return Result.fail("分类名称不能为空");
            }
            questionCategoryMapper.insert(category);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.fail("添加失败: " + e.getMessage());
        }
    }

    /**
     * 更新问题分类
     */
    @PutMapping("/update")
    public Result updateCategory(@RequestBody QuestionCategory category) {
        try {
            if (category.getId() == 0) {  // 基本类型int默认为0，不能为null
                return Result.fail("分类ID不能为空");
            }
            if (category.getQuestionCategory() == null || category.getQuestionCategory().trim().isEmpty()) {
                return Result.fail("分类名称不能为空");
            }
            questionCategoryMapper.updateById(category);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.fail("更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除问题分类
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteCategory(@PathVariable Integer id) {
        try {
            if (id == 0) {  // 基本类型int默认为0，不能为null
                return Result.fail("分类ID不能为空");
            }
            questionCategoryMapper.deleteById(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.fail("删除失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取问题分类
     */
    @GetMapping("/{id}")
    public Result getCategoryById(@PathVariable Integer id) {
        try {
            QuestionCategory category = questionCategoryMapper.selectById(id);
            if (category == null) {
                return Result.fail("未找到该分类");
            }
            return Result.success(category);
        } catch (Exception e) {
            return Result.fail("获取分类信息失败: " + e.getMessage());
        }
    }
}
